package com.hmall.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.exception.BusinessException;
import com.hmall.user.domain.dto.AddRoleDTO;
import com.hmall.user.domain.dto.UpdateRoleDTO;
import com.hmall.user.domain.po.SysPermissions;
import com.hmall.user.domain.po.SysRolePermissions;
import com.hmall.user.domain.po.SysRoles;
import com.hmall.user.domain.po.SysUserRoles;
import com.hmall.user.service.SysPermissionsService;
import com.hmall.user.service.SysRolePermissionsService;
import com.hmall.user.service.SysRolesService;
import com.hmall.user.mapper.SysRolesMapper;
import com.hmall.user.service.SysUserRolesService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author yangfeng
* @description 针对表【sys_roles(角色管理信息表)】的数据库操作Service实现
* @createDate 2024-12-17 16:36:29
*/
@Service
public class SysRolesServiceImpl extends ServiceImpl<SysRolesMapper, SysRoles>
    implements SysRolesService{
    @Resource
    private SysUserRolesService sysUserRolesService;
    @Resource
    private SysRolePermissionsService sysRolePermissionsService;
    @Resource
    private SysPermissionsService sysPermissionsService;

    @Override
    public Boolean addRole(AddRoleDTO dto) {
        SysRoles roles = BeanUtil.toBean(dto, SysRoles.class);
        return save(roles);
    }

    @Override
    public Boolean updateRole(UpdateRoleDTO dto) {
        SysRoles role = getById(dto.getId());
        if(ObjectUtil.isNull(role)){
            throw new BusinessException("角色不存在");
        }
        BeanUtil.copyProperties(dto,role,true);
        return updateById(role);
    }
    @Cacheable(value = "Roles",key = "'userId-'+#userId",unless = "#result == null",cacheManager = "redisCacheManager")
    @Override
    public List<Integer> getRolesByUserId(Long userId) {
        List<SysUserRoles> list = sysUserRolesService.lambdaQuery().eq(SysUserRoles::getUserId, userId).list();
        if(CollectionUtil.isNotEmpty(list)){
            List<Integer> roleIds = list.stream().map(SysUserRoles::getRoleId).collect(Collectors.toList());
            return roleIds;
        }
        return null;
    }
    @Cacheable(value = "Roles",key = "'role-'+#id",unless = "#result == null",cacheManager = "redisCacheManager")
    @Override
    public SysRoles getRoleById(Integer id) {
        return getById(id);
    }
    @Cacheable(value = "User-Permissions",key = "'userId-'+#userId",unless = "#result == null",cacheManager = "redisCacheManager")
    @Override
    public List<SysPermissions> getPermissionByUserId(Long userId) {
        List<Integer> roleIds = getRolesByUserId(userId);
        List<SysPermissions> permissions = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(roleIds)){
            roleIds.forEach(roleId->{
                List<SysPermissions> rolePermissions = getPermissionByRoleId(roleId);
                if(CollectionUtil.isNotEmpty(rolePermissions)){
                    permissions.addAll(rolePermissions);
                }
            });
        }
        return permissions;
    }
    @Cacheable(value = "Roles-Permission",key = "'role-'+#roleId",unless = "#result == null",cacheManager = "redisCacheManager")
    @Override
    public List<SysPermissions> getPermissionByRoleId(Integer roleId) {
        List<SysRolePermissions> rolePermissions = sysRolePermissionsService.lambdaQuery().eq(SysRolePermissions::getRoleId, roleId).list();
        if(CollectionUtil.isNotEmpty(rolePermissions)){
            List<Integer> permissionIds = rolePermissions.stream().map(SysRolePermissions::getPermissionId).collect(Collectors.toList());
            return sysPermissionsService.lambdaQuery().in(SysPermissions::getId,permissionIds).list();
        }
        return null;
    }

    @Override
    public List<SysPermissions> getPermissionByRoleId(Integer... roleIds) {
        List<SysPermissions> list = new ArrayList<>();
        if(ObjectUtil.isNotNull(roleIds) && roleIds.length>1){
            for (Integer roleId : roleIds) {
                List<SysPermissions> permission = getPermissionByRoleId(roleId);
                if(CollectionUtil.isNotEmpty(permission)){
                    list.addAll(permission);
                }
            }
        }
        return list;
    }

//    @Override
//    public boolean updateById(SysRoles entity) {
//        entity.setUpdatedAt(null);
//        return super.updateById(entity);
//    }
}




